package com.beiing.tianshuai.tianshuai.freshnews.presenter;

import com.beiing.tianshuai.tianshuai.dongtai.CallBack;
import com.beiing.tianshuai.tianshuai.entity.ActiveReplyBean;
import com.beiing.tianshuai.tianshuai.entity.BaseBean;
import com.beiing.tianshuai.tianshuai.entity.CodeBean;
import com.beiing.tianshuai.tianshuai.entity.DiscoveryCommentBean;
import com.beiing.tianshuai.tianshuai.entity.DiscoveryCommentListBean;
import com.beiing.tianshuai.tianshuai.entity.DiscoveryDetailsBean;
import com.beiing.tianshuai.tianshuai.entity.DiscoveryIsPraiseBean;
import com.beiing.tianshuai.tianshuai.entity.DiscoveryPraiseBean;
import com.beiing.tianshuai.tianshuai.entity.DynamicDeleteBean;
import com.beiing.tianshuai.tianshuai.entity.FollowBean;
import com.beiing.tianshuai.tianshuai.freshnews.model.DiscoveryDetailModel;
import com.beiing.tianshuai.tianshuai.freshnews.model.DiscoveryDetailModelImpl;
import com.beiing.tianshuai.tianshuai.freshnews.view.DiscoveryDetailViewImpl;
import com.beiing.tianshuai.tianshuai.rx.MsgEvent;
import com.beiing.tianshuai.tianshuai.rx.RxBus;

/**
 * Created by LZQ on 2017/7/28.
 */

public class DiscoveryDetailPresenter implements DiscoveryDetailPresenterImpl {

    private DiscoveryDetailModelImpl mModel;
    private DiscoveryDetailViewImpl mView;

    public DiscoveryDetailPresenter(DiscoveryDetailViewImpl mView) {
        this.mView = mView;
        this.mModel = new DiscoveryDetailModel();
    }

    @Override
    public void getDiscoveryDetail(String f_id) {
        mView.showProgress();
        mModel.getDiscoveryDetail(f_id, new CallBack<DiscoveryDetailsBean>() {

            @Override
            public void result(DiscoveryDetailsBean bean) {
                mView.hideProgress();
                mView.showDiscoveryDetail(bean);
            }

            @Override
            public void error(Throwable e) {

            }
        });
    }

    @Override
    public void getDiscoveryCommentList(String f_id, int page, int num, int type) {

        mModel.getDiscoveryCommentList(f_id, page, num, type, new CallBack<DiscoveryCommentListBean>() {
            @Override
            public void result(DiscoveryCommentListBean bean) {
                mView.showDiscoveryCommentList(bean);
            }

            @Override
            public void error(Throwable e) {
                mView.showError(0);
            }
        });
    }

    @Override
    public void commentDiscovery(String u_id, final String f_id, String review) {
        mModel.commentDiscovery(u_id, f_id, review, new CallBack<DiscoveryCommentBean>() {

            @Override
            public void result(DiscoveryCommentBean bean) {
                mView.refreshComment();
                getDiscoveryCommentList(f_id, 0, 5, 0);
            }

            @Override
            public void error(Throwable e) {

            }
        });
    }

    @Override
    public void getDiscoveryIsPraise(String u_id, String f_id) {
        mModel.getDiscoveryIsPraise(u_id, f_id, new CallBack<DiscoveryIsPraiseBean>() {

            @Override
            public void result(DiscoveryIsPraiseBean bean) {
                mView.showIsPraiseOrNot(bean);
            }

            @Override
            public void error(Throwable e) {

            }
        });
    }

    @Override
    public void praiseDiscovery(final String u_id, final String f_id, final int position) {
        mModel.praiseDiscovery(u_id, f_id, new CallBack<DiscoveryPraiseBean>() {

            @Override
            public void result(DiscoveryPraiseBean bean) {
                mView.showPraisedResult(bean);
                if (position != -1) {
                    RxBus.getInstanceBus().post(new MsgEvent<>("点赞", bean.getCode(), position));
                }
            }

            @Override
            public void error(Throwable e) {

            }
        });
    }

    @Override
    public void praiseComment(final String f_id, String u_id, String c_id) {
        mModel.praiseComment(f_id, u_id, c_id, new CallBack<DiscoveryPraiseBean>() {

            @Override
            public void result(DiscoveryPraiseBean bean) {
                getDiscoveryCommentList(f_id, 0, 5, 0);
            }

            @Override
            public void error(Throwable e) {

            }
        });
    }

    @Override
    public void isFollowed(String fromUid, String toUid) {
        mModel.isFollowed(fromUid, toUid, new CallBack<FollowBean>() {

            @Override
            public void result(FollowBean bean) {
                mView.showMorePopupWindow(bean);
            }

            @Override
            public void error(Throwable e) {

            }
        });
    }

    @Override
    public void setFollowed(String fromUid, String toUid) {
        mModel.setFollowed(fromUid, toUid, new CallBack() {
            @Override
            public void result(BaseBean bean) {

            }

            @Override
            public void error(Throwable e) {

            }
        });
    }

    @Override
    public void deleteComment(String id) {
        mModel.deleteComment(id, new CallBack<CodeBean>() {

            @Override
            public void result(CodeBean bean) {

            }

            @Override
            public void error(Throwable e) {

            }
        });
    }

    @Override
    public void addReply(final String fid, String cid, String form_uid, String to_uid, String content, String comment, int type, String rid) {
        mModel.addReply(fid, cid, form_uid, to_uid, content, comment,type, rid, new CallBack<ActiveReplyBean>() {

            @Override
            public void result(ActiveReplyBean bean) {
                getDiscoveryCommentList(fid, 0, 5, 0);
            }

            @Override
            public void error(Throwable e) {

            }
        });
    }

    @Override
    public void deleteReply(String fid, String id, String uid) {
        mModel.deleteReply(fid, id, uid, new CallBack<DynamicDeleteBean>() {

            @Override
            public void result(DynamicDeleteBean bean) {

            }

            @Override
            public void error(Throwable e) {

            }
        });
    }

    @Override
    public void praiseReply(final String fid, String cid, String rid, String uid, String to_uid) {
        mModel.praiseReply(fid, cid, rid, uid, to_uid, new CallBack<DiscoveryPraiseBean>() {

            @Override
            public void result(DiscoveryPraiseBean bean) {
                getDiscoveryCommentList(fid, 0, 5, 0);
            }

            @Override
            public void error(Throwable e) {

            }
        });
    }
}
